package com.ol.edu.server.service.advertise.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ol.edu.server.entity.PromotionAd;
import com.ol.edu.server.mapper.PromotionAdMapper;
import com.ol.edu.server.service.advertise.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Roger
 * @date 2020/9/7 10:58
 * @description
 */
@Service
public class PromotionAdServiceImpl implements PromotionAdService {

    @Autowired
    private PromotionAdMapper promotionAdMapper;
    /*
        分页获取广告列表数据
     */
    @Override
    public PageInfo findAllPromotionAdByPage(Integer currentPage, Integer pageSize) {
        //获取分页数据
        PageHelper.startPage(currentPage, pageSize);
        List<PromotionAd> promotionAdList = promotionAdMapper.findAllPromotionAdByPage();
        PageInfo<PromotionAd> pageInfo = new PageInfo<>(promotionAdList);

        return pageInfo;
    }
    /*
        新建广告接口
     */
    @Override
    public void savePromotionAd(PromotionAd promotionAd) {
        Date date = new Date();
        promotionAd.setCreateTime(date);
        promotionAd.setUpdateTime(date);

        promotionAdMapper.savePromotionAd(promotionAd);

    }
    /*
        修改广告接口
     */
    @Override
    public void updatePromotion(PromotionAd promotionAd) {
        promotionAd.setUpdateTime(new Date());

        promotionAdMapper.updatePromotion(promotionAd);
    }
    /*
        接收广告ID,返回广告详细信息
     */
    @Override
    public PromotionAd findPromotionAdById(Integer id) {
        PromotionAd promotionAd = promotionAdMapper.findPromotionAdById(id);
        return promotionAd;
    }
    /*
        修改广告上下线状态
     */
    @Override
    public Map updatePromotionAdStatus(PromotionAd promotionAd) {
        promotionAd.setUpdateTime(new Date());

        Integer res = promotionAdMapper.updatePromotionAdStatus(promotionAd);
        if (res == 1) {
            Map<String, Integer> map = new HashMap<>(1);
            map.put("status",promotionAd.getStatus());
            return map;
        }
        return null;
    }
}
